package com.yakisoft.jublish;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class SiteConfig {
	public String server;
	public String username;
	public String password;
	public String destinationFolder;
	public String sourceFolder;
	public List<String> ignored = new ArrayList<String>();
	public Map<String, String> files = new HashMap<String, String>();

	public SiteConfig(Reader reader) throws IOException {
		BufferedReader bufferedReader = new BufferedReader(reader);
		readConfigFile(bufferedReader);
	}

	private void readConfigFile(BufferedReader bufferedReader) throws IOException {
		String line;
		while ((line = bufferedReader.readLine()) != null) {
			if (line.startsWith("server:")) {
				server = line.substring("server:".length());
			}
			else if (line.startsWith("user:")){
				username = line.substring("user:".length());
			}
			else if (line.startsWith("password:")) {
				password = line.substring("password:".length());
			}
			else if (line.startsWith("destination-folder:")) {
				destinationFolder = line.substring("destination-folder:".length());
			}
			else if (line.startsWith("source-folder:")) {
				sourceFolder = line.substring("source-folder:".length());
			}
			else if (line.startsWith("ignore:")) {
				ignored.add(line.substring("ignore:".length()));
			}
			else if (line.startsWith("file:")) {
				line = line.substring("file:".length());
				int pos = line.lastIndexOf(' ');
				if (pos == -1) {
					throw new IOException("Error reading file information");
				}

				files.put(line.substring(0, pos), line.substring(pos + 1));
			}
		}
	}

	public boolean isIgnored(String relativePath) {
		GlobMatch match = new GlobMatch();
		for(String pattern : ignored) {
			if (match.match(relativePath, pattern)) {
				return true;
			}
		}

		return false;
	}

	public void publish() throws IOException {
		publishFolder("");
	}

	private void publishFolder(String relative) throws IOException {
		File directory = new File(this.sourceFolder, relative);
		System.out.println("Dir:  " + directory);
		  
		if (!directory.isDirectory()) {
			throw new IOException("Not a directory");
		}

		File[] files = directory.listFiles();
		for(File file : files) {
			if (isIgnored(file.getCanonicalPath())) {
				continue;
			}

			if (file.isDirectory()) {
				if (isUpdated(relative + File.separator + file.getName())) {
					// TODO Create remote directory

					// TODO Update hash (could be anything for a folder)
				}
				publishFolder(relative + File.separator + file.getName());
			}

			if (file.isFile() && isUpdated(relative + File.separator + file.getName())) {
				// TODO Upload file

				// TODO Update hash
				System.out.println("File: " + file.getCanonicalPath());
			}
		}
	}

	private boolean isUpdated(String relativePath) {
		if (!files.containsKey(relativePath)) {
			// If we don't have a hash then the file must be new
			return true;
		}

		return !files.get(relativePath).equals(getMD5(relativePath));
	}

	private String getMD5(String relativePath) {
		// TODO Get MD5 of file
		return "01234";
	}
}
